define(['egis'], function (egis) {
    Window.deserializer = Window.deserializer || new egis.core.Deserializer();
    Window.container = Window.container || {};
    dsBridge.register('create_ChartRenderer', function (sourceId, mapId, fields, colors, chart) {
        var statChart = JSON.parse(chart);
        var map = Window.container[mapId];
        try{
            var options = {
                map:  map,
                chartTemplate: createTemplate(fields, colors, statChart)
            };
            var chartRenderer = new egis.render.ChartRenderer(options);
            Window.container[sourceId] = chartRenderer;
        }
        catch (e){
            alert(e);
        }
    })

    function createTemplate(fields, colors, statChart) {
        fields = Window.deserializer.createJsInstance(JSON.parse(fields));
        var charttype = statChart.charttype;
        switch (charttype){
            case 'BarChart':
                var chart = new egis.render.BarChart({
                    fields: fields.fields,
                    options:{
                        color: colors,
                        name: statChart.name,
                        itemStyle:{
                            barBorderColor: Window.deserializer.createJsInstance(statChart.barBorderColor).toHex(),
                            label: statChart.label
                        }
                    }
                });
                return chart;
            case 'LineChart':
                var chart = new egis.render.LineChart({
                    fields: fields.fields,
                    options:{
                        color: colors,
                        name: statChart.name
                    }
                });
                return chart;
            case 'PieChart':
                var chart = new egis.render.PieChart({
                    fields: fields.fields,
                    options:{
                        type:1,
                        color: colors,
                        name: statChart.name,
                        radius: statChart.radius,
                        labelStyle: Window.deserializer.createJsInstance(statChart.labelColor).toHex()
                    }
                });
                return chart;

        }
    }

})